package com.qingyun.video.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingyun.apis.user.IUserClient;
import com.qingyun.model.user.pojos.User;
import com.qingyun.model.video.pojos.Reply;
import com.qingyun.video.mapper.ReplyMapper;
import com.qingyun.video.service.ReplyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;


@Service
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {

    @Resource
    private IUserClient userClient;
    @Override
    public List<Reply> listParentComments(Long blogId) {

        List<Reply> list = query().isNull("parent_id").eq("blog_id", blogId).list();
        for (Reply blogReply : list) {
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(blogReply.getUserId()).getData();
            User user = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            blogReply.setIcon(user.getImage());
            blogReply.setNickname(user.getName());

        }
        return list;
    }

    @Override
    public List<Reply> listChildComments(Long parentId,Long blogId) {
        List<Reply> list = query().isNotNull("parent_id").eq("blog_id", blogId).eq("parent_id",parentId).list();
        for (Reply blogReply : list) {
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(blogReply.getUserId()).getData();
            User user = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            blogReply.setIcon(user.getImage());
            blogReply.setNickname(user.getName());
            LinkedHashMap<String, Object> data1 = (LinkedHashMap<String, Object>) userClient.information(blogReply.getReplyId()).getData();
            User user1 = JSONUtil.toBean(JSONUtil.toJsonStr(data1), User.class);

            blogReply.setReplyName(user1.getName());

        }
        return list;
    }
}
